
/*
 *  
 *
 *  Copyright (C) Microsoft Corporation.  All rights reserved
 *
 *  
 */


namespace CRMScenarios
{
    using System;
    using Extensibility;
    using System.Runtime.InteropServices;
    // For Excel Object model.
    using Excel = Microsoft.Office.Interop.Excel;

    // For Word Object model.
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;

	#region Read me for Add-in installation and setup information.
	// When run, the Add-in wizard prepared the registry for the Add-in.
	// At a later time, if the Add-in becomes unavailable for reasons such as:
	//   1) You moved this project to a computer other than which is was originally created on.
	//   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
	//   3) Registry corruption.
	// you will need to re-register the Add-in by building the CRMScenariosSetup project, 
	// right click the project in the Solution Explorer, then choose install.
	#endregion
	
	/// <summary>
	///   The object for implementing an Add-in.
	/// </summary>
	/// <seealso class='IDTExtensibility2' />
	[GuidAttribute("F9682B7F-A7C9-4774-A79F-C50211B49BAE"), ProgId("CRMScenarios.Connect")]
    public class Connect : Object, Extensibility.IDTExtensibility2
    {
        /// <summary>
        /// Connect: Constructor of the addin
        /// Things to be done exactly once can be done here. 
        /// </summary>
        public Connect()
        {
            Helper.GetTrace();
            Helper.Trace("public Connect()", true);
        }

        /// <summary>
        /// OnConnection: Called after the application is just started.
        /// </summary>
        /// <param name="application">The application object. Could be word/excel</param>
        /// <param name="connectMode"></param>
        /// <param name="addInInst"></param>
        /// <param name="custom"></param>
        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
        {
            // Do a sanity check.
            Helper.Trace("public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)", true);
            if ((application != null) && ((application is Word.Application) || (application is Excel.Application)))
            {
                // Now application is there in the global variable.
                applicationObject = application;
                addInInstance = addInInst;
            }
        }


        /// <summary>
        /// OnDisconnection: Just before exiting. 
        /// </summary>
        /// <param name="disconnectMode"></param>
        /// <param name="custom"></param>
        public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
        {
            Helper.Trace("public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)", true);
            if (applicationObject == null)
            {
                return;
            }
        }

        /// <summary>
        /// If some changes are to be done during the execution.
        /// </summary>
        /// <param name="custom"></param>
        public void OnAddInsUpdate(ref System.Array custom)
        {
            Helper.Trace("public void OnAddInsUpdate(ref System.Array custom)", true);
        }

        /// <summary>
        /// Once the startup of the application is over, this fucntion is called. 
        /// Here is where we do the necessary stuff.
        /// </summary>
        /// <param name="custom"></param>
        public void OnStartupComplete(ref System.Array custom)
        {
            Helper.Trace("public void OnStartupComplete(ref System.Array custom)", true);
            if (applicationObject == null)
            {
                return;
            }
            if (applicationObject is Word.Application)
            {
                WordHelper.Initialization((Word.Application)applicationObject);
            }
            else if (applicationObject is Excel.Application)
            {
                ExcelHelper.Initialization((Excel.Application)applicationObject);
            }
        }


        /// <summary>
        /// We are about to quit the application.
        /// </summary>
        /// <param name="custom"></param>
        public void OnBeginShutdown(ref System.Array custom)
        {
            Helper.Trace("public void OnBeginShutdown(ref System.Array custom)", true);
            if (applicationObject == null)
            {
                return;
            }
            if (applicationObject is Word.Application)
            {
                WordHelper.cleanUpApplication();
            }
            else if (applicationObject is Excel.Application)
            {
                ExcelHelper.cleanUpApplication();
            }
        }

        // Dont we need a handle to the application object? 
        private object applicationObject;
        // I really dont know why this is used.
        private object addInInstance;
    }
}